package com.luozongyao.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luozongyao.statistics.entity.Statistic;
import com.luozongyao.statistics.mapper.StatisticMapper;
import com.luozongyao.statistics.service.ReportUserService;
import com.luozongyao.statistics.service.ReportVideoService;
import com.luozongyao.statistics.service.StatisticService;
import com.luozongyao.statistics.service.UserService;
import com.luozongyao.videoPromotion.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luozongyao
 * @since 2021-11-13
 */
@Service
public class StatisticServiceImpl extends ServiceImpl<StatisticMapper, Statistic> implements StatisticService {



    @Autowired
    @Lazy
    private UserService userService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private ReportVideoService reportVideoService;

    @Autowired
    private ReportUserService reportUserService;

    @Autowired
    private RedisTemplate stringRedisTemplate;

    @Override
    public void statisticCount(String day) {
        //删除已存在的统计对象 防止相同的记录出现
        QueryWrapper<Statistic> queryWrapper = new QueryWrapper<Statistic>();
        queryWrapper.eq("statistic_date",day);
        baseMapper.delete(queryWrapper);
        //获取统计信息
        Integer registerCount = userService.registerCount(day);
        Integer videoCount = videoService.getVideoCount(day);
        Integer reportUserCount = reportUserService.getReportUserCount(day);
        Integer reportVideoCount = reportVideoService.getReportVideoCount(day);

//        Long size = stringRedisTemplate.opsForSet().size("daily:activity:"+"2021-11-16" );
        Long size = stringRedisTemplate.opsForSet().size("daily:activity:"+day );
        Integer loginNum = size.intValue();
        //添加统计数据信息
        Statistic statisticsDaily = new Statistic();
        statisticsDaily.setStatisticDate(day);
        statisticsDaily.setStatisticLoginNum(loginNum);
        statisticsDaily.setStatisticRegisterNum(registerCount);
        statisticsDaily.setStatisticVideoPublish(videoCount);
        statisticsDaily.setStatisticReportedUser(reportUserCount);
        statisticsDaily.setStatisticReportedVideo(reportVideoCount);
        baseMapper.insert(statisticsDaily);

    }
    //获取echarts图表所需的数据 并返回
    @Override
    public Map<String, Object> getChartData(String type, String begin, String end) {
        QueryWrapper<Statistic> wrapper = new QueryWrapper<Statistic>();
        wrapper.between("statistic_date",begin,end);
//        if (!StringUtils.isEmpty(begin)){
//            wrapper.ge("statistic_date",begin);
//        }
//        if (!StringUtils.isEmpty(end)){
//            wrapper.le("statistic_date",end);
//        }

        wrapper.orderByAsc("statistic_date");
        List<Statistic> statisticList = baseMapper.selectList(wrapper);
        //因为返回有两部分数据:日期和日期对应数量
        Map<String, Object> map = new HashMap<String, Object>();
        List<Integer> dataList = new ArrayList<Integer>();
        List<String> dateList = new ArrayList<String>();
        map.put("dataList", dataList);
        map.put("dateList", dateList);

        for (int i = 0; i < statisticList.size(); i++) {
            Statistic statistic = statisticList.get(i);
            dateList.add(statistic.getStatisticDate());
            //判断查询的条件是什么类型 登录数量 注册数量 视频发布数 用户举报数 视频举报数
            switch (type){
                case "login_num":
                    dataList.add(statistic.getStatisticLoginNum());
                    break;
                case "register_num":
                    dataList.add(statistic.getStatisticRegisterNum());
                    break;
                case "video_num":
                    dataList.add(statistic.getStatisticVideoPublish());
                    break;
                case "report_user":
                    dataList.add(statistic.getStatisticReportedUser());
                    break;
                case "report_video":
                    dataList.add(statistic.getStatisticReportedVideo());
                    break;
                default:
                    break;
            }
        }

        return map;
    }
}
